<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';

// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	if(!isset($post_data['addwedding-step'])) $post_data['addwedding-step'] = 'compte';
	// empty post data verification
	if(empty($post_data['firstName']) || empty($post_data['lastName'])
	|| empty($post_data['dd']) || empty($post_data['mm'])
	|| empty($post_data['yyyy']) || empty($post_data['gender'])) {
		$errors_text .= "Des champs sont vides<br />";
		$errors++;
	}
	
	// birthday verification (is valid date)
	if(!checkdate($post_data['mm'], $post_data['dd'], $post_data['yyyy'])) {
		$errors_text .= "La date de naissance n'est pas valide<br />";
		$errors++;
	}
	
	// phone verification (10 digits)
	if(!empty($post_data['phoneNumber'])) {
		if(!check_phoneNumber($post_data['phoneNumber'])) {
			$errors_text .= "Le téléphone est incorrect<br />";
			$errors++;
		}
		if(substr($post_data['phoneNumber'], 0, 2) != '06' && substr($post_data['phoneNumber'], 0, 2) != '07') {
			$errors_text .= "Le téléphone doit commencer par 06 ou 07<br />";
			$errors++;
		}
	}
	
	if($post_data['addwedding-step'] != 'step-1'&& $post_data['addwedding-step'] != 'step-2'){
		// checkTerms verification
		if($post_data['checkTerms'] != '0' && $post_data['checkTerms'] != '1') {
			$errors_text .= "La checkTerms n'est pas valide<br />";
			$errors++;
		}
	}
	
	// email verification
	/*
	if(!check_email($post_data['email']) ) {
		$errors_text .= "L'email n'est pas correct<br />";
		$errors++;
	}*/
	// gender verification (is 'MALE' or 'FEMALE')
	if(!check_gender($post_data['gender'])) {
		$errors_text .= "La civilité n'est pas valide<br />";
		$errors++;
	}
	// check if user exists
	/*
	if(!check_userexist($post_data['email'])) {
		$errors_text .= "User already exists.<br />";
		$errors++;
	}*/
	
	// if no errors we proceed
	if($errors == 0) {
		// data correction
		$firstName = ucfirst(strtolower($post_data["firstName"]));
		$lastName = strtoupper($post_data["lastName"]);
		//$email = strtolower(filter($post_data["email"]));
		$wid = $_SESSION['wid'];
		if($post_data['addwedding-step'] == 'step-1') {
			$uid = $_SESSION['luid'];
		} elseif($post_data['addwedding-step'] == 'step-2') {
			$uid = $_SESSION['ruid'];
		} else $uid = $_SESSION['guid'];
		
		$phoneNumber = $post_data["phoneNumber"];
		
		//Update T_PERSON
		if($post_data['addwedding-step'] != 'step-1'&& $post_data['addwedding-step'] != 'step-2'){
			$update_sql = "
			UPDATE `T_PERSON` SET
			`FIRST_NAME`='".$firstName."',`SURNAME`='".$lastName."',
			`PHONE_NUMBER`='".$phoneNumber."',`GENDER`='".$post_data['gender']."',`WANT_NEWS` ='".$post_data['checkTerms']."'
			WHERE `PERSON_SID`='".$uid."'"
			;
		} else {
			$birthDate = $post_data["yyyy"].'-'.$post_data["mm"].'-'.$post_data["dd"];
			$update_sql = "
			UPDATE `T_PERSON` SET
			`FIRST_NAME`='".$firstName."',`SURNAME`='".$lastName."',
			`BIRTHDATE`='".$birthDate."',`PHONE_NUMBER`='".$phoneNumber."',`GENDER`='".$post_data['gender']."'
			WHERE `PERSON_SID`='".$uid."'"
			;
		}
		if(!mysql_query($update_sql)) {
			$errors_text .= mysql_error()."<br />";
			$errors++;
		}
		
		//Update T_WEDDINGS
		if($post_data['addwedding-step'] == 'step-1') {
			$update_sql2 = "
			UPDATE `T_WEDDINGS` SET
			`LEFT_LIKES`='".$post_data['like']."',`LEFT_DISLIKES`='".$post_data['not_like']."' WHERE `WEDDING_ID`='".$wid."'"
			;
			if(!mysql_query($update_sql2)) {
				$errors_text .= mysql_error()."<br />";
				$errors++;
			}
		} elseif($post_data['addwedding-step'] == 'step-2') {
			$update_sql2 = "
			UPDATE `T_WEDDINGS` SET
			`RIGHT_LIKES`='".$post_data['like']."',`RIGHT_DISLIKES`='".$post_data['not_like']."' WHERE `WEDDING_ID`='".$wid."'"
			;
			if(!mysql_query($update_sql2)) {
				$errors_text .= mysql_error()."<br />";
				$errors++;
			}
		}

	}
	
	if($errors == 0) {
		$arr['success'] = "1"; 
        $arr['msg'] = 'Vos informations ont bien été enregistrées.';
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>